package com.ms.education.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ms.education.acl.entity.RolePermission;
import com.ms.education.acl.mapper.RolePermissionMapper;
import com.ms.education.acl.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-02-18
 */
@Service
public class RolePermissionServiceImp extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Transactional
    @Override
    public void saveRolePermissionRelationShip(Integer roleId, Integer[] permissionIds) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<RolePermission>().eq("role_id", roleId);
        remove(wrapper);
        List<RolePermission> rolePermissions = Arrays.stream(permissionIds).map(rp -> {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(rp);
            return rolePermission;
        }).collect(Collectors.toList());
        saveBatch(rolePermissions);
    }
}
